package com.ycsj.common.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.blankj.utilcode.util.StringUtils;
import com.ycsj.common.base.BaseApplication;

import static com.ycsj.common.base.BaseApplication.context;

/**
 * Created by Administrator on 2016/3/8.
 * 这个工具类辅助获取一些相关的XMl文件以及XML资源
 */
public class UiUtils {
    private static Toast toast;
    private static final int CLICK_DELAY_TIME = 500;
    private static long lastClickTime;
    /**
     * 通过布局文件ID获取视图
     *
     * @param layoutId
     * @return
     */
    public static View getView(int layoutId) {
        Log.i("test", context + "");
        View view = View.inflate(getContext(), layoutId, null);
        return view;
    }

    /**
     * 获取整个应用程序的上下文对象
     *
     * @return
     */
    public static Context getContext() {
        return context;
    }

    /**
     * 获取color.xml文件中的color资源
     *
     * @param colorId
     * @return
     */
    public static int getColor(int colorId) {
        return getContext().getResources().getColor(colorId);
    }


    /**
     * 判断App是否是Debug版本
     *
     * @return {@code true}: 是<br>{@code false}: 否
     */
    public static boolean isAppDebug() {
        if (StringUtils.isSpace(getContext().getPackageName())) return false;
        try {
            PackageManager pm = context.getPackageManager();
            ApplicationInfo ai = pm.getApplicationInfo(context.getPackageName(), 0);
            return ai != null && (ai.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return false;
        }
    }


    /**
     * 获取自定义属性的数组
     *
     * @param strArrId
     * @return
     */
    public static String[] getStringArr(int strArrId) {
        return getContext().getResources().getStringArray(strArrId);
    }

    /**
     * 定义吐司的方法,防止吐司重复弹出的解决办法
     *
     * @param toastText
     * @param flag
     */
    public static void Toast(String toastText, boolean flag) {

        if (toast == null) {
            if (flag) {
                toast = Toast.makeText(getContext(), toastText, Toast.LENGTH_LONG);
            } else {
                toast = Toast.makeText(getContext(), toastText, Toast.LENGTH_SHORT);
            }
        } else {
            if (flag) {
                toast.setText(toastText);
                toast.setDuration(Toast.LENGTH_LONG);
            } else {
                toast.setText(toastText);
                toast.setDuration(Toast.LENGTH_LONG);
            }
        }
        toast.show();
    }

    /**
     * 重载方法,用于吐司在屏幕中间显示的方法
     *
     * @param toastText
     * @param flag
     */
    public static void ToastCenter(String toastText, boolean flag) {
        if (toast == null) {
            if (flag) {
                toast = Toast.makeText(getContext(), toastText, Toast.LENGTH_LONG);
            } else {
                toast = Toast.makeText(getContext(), toastText, Toast.LENGTH_SHORT);
            }
        } else {
            if (flag) {
                toast.setText(toastText);
                toast.setDuration(Toast.LENGTH_LONG);
            } else {
                toast.setText(toastText);
                toast.setDuration(Toast.LENGTH_LONG);
            }
        }
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }

    /**
     * 手动关闭吐司的方法
     */
    public static void cancelToast() {
        if (toast != null) {
            toast.cancel();
        }
    }


    /**
     * 定义将dp转化为px的方法
     *
     * @param dp
     * @return
     */
    public static int dp2px(int dp) {
        float density = getContext().getResources().getDisplayMetrics().density;
        return (int) (dp * density + 0.5f);
    }

    /**
     * 定义将px转化为dp的方法
     *
     * @param px
     * @return
     */
    public static int px2dp(int px) {
        float density = getContext().getResources().getDisplayMetrics().density;
        return (int) (px / density + 0.5f);
    }



    /**
     * 获取到应用程序的Handler对象
     *
     * @return
     */
    public static Handler getHandler() {
        return BaseApplication.handler;
    }


    /**
     * 将Runnable线程放入到主线程运行的方法
     *
     * @param runnable
     */
    public static void runOnUiThread(Runnable runnable) {
        if (isInMainThread()) {
            runnable.run();
        } else {
            getHandler().post(runnable);
        }
    }

    /**
     * 判断当线运行的线程是否是主线程ID
     *
     * @return
     */
    public static boolean isInMainThread() {
        int myTid = android.os.Process.myTid();
        return myTid == BaseApplication.mainThreadId;
    }

    /**
     * 改变屏幕透明度的方法
     *
     * @param activity
     * @param bgAlpha
     */
    public static void backgroundAlpha(Activity activity, float bgAlpha) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        activity.getWindow().setAttributes(lp);
    }

    /**
     * Activity进行跳转的方法,注意设置了标记,如果是跳转系统相机，不能用这个标记,否则拿不到返回码
     */
    public static void startActivity(Context context, Class<? extends Activity> clazz) {
        Intent intent = new Intent(context, clazz);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        context.startActivity(intent);
    }


    /**
     * 跳转到目标activity
     *
     * @param cls
     * @param bundle
     */
    public static void goToActivity(Class cls, Bundle bundle) {
        Intent intent = new Intent(getContext(), cls);
        //携带数据的跳转,如果没有数据需要携带,直接传null即可
        if (bundle != null) {
            intent.putExtra("bundle", bundle);
        }
        getContext().startActivity(intent);
    }

    /**
     * 隐藏系统键盘的方法
     */
    public static void hideKeyboard(Activity activity) {
        View view = activity.getCurrentFocus();
        if (view != null) {
            ((InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE)).
                    hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }


    /**
     * 测量View的宽高
     *
     * @param view View
     */
    public static void measureWidthAndHeight(View view) {
        int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        view.measure(widthMeasureSpec, heightMeasureSpec);
    }


    /**
     * 防止button快速连点
     *
     * @return
     */
    public static boolean isNotFastClick() {
        boolean flag = false;
        long currentClickTime = System.currentTimeMillis();
        if ((currentClickTime - lastClickTime) >= CLICK_DELAY_TIME) {
            flag = true;
        }
        lastClickTime = currentClickTime;
        return flag;
    }
}
